﻿using HBase;
using hconnect;
using MongoDB.Bson;
using System.Collections.Generic;
using System;

namespace Controller
{
    public class CraneStatus : AbsStatus
    {
        public CraneStatus() : base(0)
        {
            fault = false;
        }
        public CraneStatus(int id) : base(id)
        {
            fault = false;
        }

        public CraneStatus(CraneStatus bs) : base(bs)
        {
            fault = false;
        }
        public override bool checkCrane()
        {
            if (DateTime.Now.Ticks - time.Ticks > 20000000) {
                LoggerCommon.consol(InfoC.consol_crane_error, "垛机" + eId + "状态获取超时" );
                return false;
            }
            if (error != 1)
            {
                LoggerCommon.consol(InfoC.consol_crane_error, "垛机" + eId + "异常" + error);
                return false;
            }
            if (allowSendTask != 1) {
                LoggerCommon.consol(InfoC.consol_crane_error,
                  "垛机" + id + "-" + allowSendTask + "不允许下发任务");
                return false;
            }
            if (finishTaskNo != 0 || finishFlag != 0)
            {
                LoggerCommon.consol(InfoC.consol_crane_error,
                    "垛机" + "任务号:"+finishTaskNo + "任务标志" + finishFlag);
                return false;
            }
           
            if (craneMode != 1)
            {
                LoggerCommon.consol(InfoC.consol_crane_error, "垛机" + eId + "模式" + craneMode);
                return false;
            }
            return true;
        }
        public int error;

        public int craneMode;//堆垛机模式
        public int finishFlag;//完成标志
        public int finishTaskNo;//完成任务号
        public int x;
        public int y;
        public int z;
        public int xdh;//巷道号
        public int allowSendTask;

        public byte[] b;
        public List<String> getInf()
        {
            List<String> list = new List<string>();
            foreach (var kv in CraneConst.errorDict)
            {
                int k = kv.Key;
                byte d = b[k / 10];
                int i = k % 10;
                int c = i << i;
                if ((c & d) != 0)
                {
                    list.Add(kv.Value);
                }
            }
            return list;

        }


        public override string getError()
        {
            if (error == 0)
            {
                return "垛机异常";
            }
            return null;
        }
        public void parseError(byte[] b)
        {
            this.b = b;
            time = DateTime.Now;
        }
        public void parseStatus(byte[] b)
        {
            craneMode = S7ByteUtil.ushort16(b, 0);
            finishFlag = S7ByteUtil.ushort16(b, 2);
            finishTaskNo = S7ByteUtil.int32(b, 4);
            error = S7ByteUtil.ushort16(b, 8);
            //if (craneMode != 2 || error != 1)
            //{
            //    setFault();
            //}
            z = S7ByteUtil.int32(b, 10);
            x = S7ByteUtil.int32(b, 14);
            y = S7ByteUtil.int32(b, 18);
            xdh = S7ByteUtil.ushort16(b, 22);
            if (id == 91) {
                allowSendTask = S7ByteUtil.ushort16(b, 28);
            } else {
                allowSendTask = S7ByteUtil.ushort16(b, 30);
            }
        }



        public override List<BsonDocument> getDef(BsonDocument his, BsonDocument document)
        {
            var list = new List<BsonDocument>() { };
            BsonDocument def = JsonUtil.getDef(his, document, CraneConst.nameList, CraneConst.nameList2);
            if (def != null)
            {
                def.Add("eId", eId);
                def.Add("ticks", time);
                def.Add("keyCode", JsonUtil.getStr(def, "finishTaskNo"));
                list.Add(def);
            }
            return list;
        }
    }

    public class CraneConst
    {

        public static List<string> nameList = new List<string>() { "finishTaskNo", "finishFlag", "error" };
        public static List<string> nameList2 = new List<string>() { "finishTaskNo" };

        static String errorInfo = "操作箱急停按纽故障-0.0;地面站急停故障-0.1;主控柜急停故障-0.2;堆垛机同巷道运行故障-0.3;Static_23-0.4;Static_10-0.5;Static_7-0.6;Static_8-0.7;" +
            "触摸屏停止故障-1.0;系统进入维修模式-1.1;安全门打开-1.2;Static_19-1.3;Static_16-1.4;Static_17-1.5;Static_1-1.6;Static_2-1.7;" +
            "Static_3-2.0;Static_4-2.1;Static_11-2.2;Static_12-2.3;Static_13-2.4;Static_14-2.5;出库站台检测有货-2.6;下发作业无效-2.7;" +
            "堆垛机与调度系统通讯故障-3.0;堆垛机与输送机通信故障-3.1;堆垛机与堆垛机通信故障-3.2;托盘号错-3.3;托盘号信息跟踪异常-3.4;取货无箱-3.5;双重入库-3.6;控制柜使能-3.7;" +
            "水平变频器故障-4.0;天轨变频器故障-4.1;水平货叉同时动作异常-4.2;水平驱动异常-4.3;水平天轨电机不同步-4.4;水平前限位开关故障-4.5;水平后限位开关故障-4.6;水平断路器供电异常-4.7;" +
            "天轨断路器供电异常-5.0;水平测距仪异常-5.1;水平运行反向-5.2;水平前进到最大列之前-5.3;水平后退到最小列之后-5.4;Static_14-5.5;Static_15-5.6;Static_16-5.7;" +
            "水平运行位置错误-6.0;水平巷道识别异常-6.1;水平一巷道开关异常-6.2;水平二巷道开关异常-6.3;水平前减速开关故障-6.4;水平后减速开关故障-6.5;水平定位异常-6.6;水平编码器故障-6.7;" +
            "Static_8-7.0;Static_18-7.1;Static_19-7.2;Static_20-7.3;Static_21-7.4;Static_22-7.5;Static_23-7.6;Static_24-7.7;" +
            "垂直微动超时(货叉不在中位)-8.0;垂直变频器故障-8.1;垂直前松绳开关故障-8.2;垂直后松绳开关故障-8.3;垂直前超载开关故障-8.4;垂直后超载开关故障-8.5;垂直断路器供电异常-8.6;垂直上限位开关故障-8.7;" +
            "垂直下限位开关故障-9.0;垂直测距值异常-9.1;垂直安全钳故障-9.2;垂直启动故障-9.3;垂直认址故障-9.4;垂直低位过低故障-9.5;垂直运行方向错误故障-9.6;垂直高位过高故障-9.7;" +
            "垂直运行过速故障-10.0;垂直限速器松绳开关故障-10.1;垂直限速器超速开关故障-10.2;垂直上最高层之上-10.3;垂直下最高层之下-10.4;Static_22-10.5;Static_23-10.6;Static_24-10.7;" +
            "垂直上限速开关故障-11.0;垂直下限速开关故障-11.1;垂直定位异常-11.2;Static_28-11.3;垂直左上横梁探测开关-11.4;垂直左下横梁探测开关-11.5;垂直右上横梁探测开关-11.6;垂直右下横梁探测开关-11.7;" +
            "货叉断路器故障-12.0;货叉变频器故障-12.1;货叉单伸位编码器故障-12.2;货叉双伸位编码器故障-12.3;货叉单伸位运行超时故障-12.4;货叉双伸位运行超时故障-12.5;载货台上的货物前超长故障-12.6;载货台上的货物后超长故障-12.7;" +
            "载货台上的货物左超宽故障-13.0;载货台上的货物右超宽故障-13.1;货叉左回位检测-13.2;货叉右回位检测-13.3;货叉前超长开关故障-13.4;货叉后超长开关故障-13.5;Static_15-13.6;Static_16-13.7;" +
            "货叉左到位开关故障-14.0;货叉中位开关故障-14.1;货叉右到位开关故障-14.2;货物左超高故障-14.3;货物右超高故障-14.4;载货台货物检测开关故障-14.5;双伸位取货任务，单伸位有货-14.6;双伸位放货任务，单伸位有货-14.7;" +
            "条码读错故障-15.0;货叉左探测开关故障-15.1;货叉右探测开关故障-15.2;货叉左超宽开关故障-15.3;货叉右超宽开关故障-15.4;货叉左超高开关故障-15.5;货叉右超高开关故障-15.6;Static_32-15.7";
        public static Dictionary<int, String> errorDict = new Dictionary<int, string>();
        static CraneConst()
        {
            String[] names = errorInfo.Split(';');
            foreach (String name in names)
            {
                if (name.IndexOf("Static") == 0)
                {
                    continue;
                }
                String[] kv = name.Split('-');
                String key = kv[1];
                int k = int.Parse(key.Replace(".", ""));
                errorDict.Add(k, kv[0]);
            }


        }
    }
}
